/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: EditRoleView.java
 * Date: 10/03/2011
 */

package com.ingenius.sau.client.view;

import java.util.LinkedList;
import java.util.List;

import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.MarginData;
import com.google.gwt.user.client.ui.FlexTable;
import com.ingenius.sau.client.presenter.EditRolePresenter.EditRoleDisplay;

/**
 * Class <strong>EditRoleView</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class EditRoleView extends AbstractEditView implements EditRoleDisplay {

	public static final String[] actions = { "", "Create", "Edit", "Read",
			"Delete" };
	public static final String[] modules = { "", "Users", "Guidelines",
			"Programs", "Processes", "Instruments" };

	// UI Widgets
	private final TextField<String> textName;
	private final TextArea textDescription;
	private final FlexTable table;

	// Constructor

	public EditRoleView() {
		super();
		textName = new TextField<String>();
		textDescription = new TextArea();
		table = new FlexTable();

		// TODO i18n
		setHeading("Edit Role");
		setSize(500, 420);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.view.AbstractEditView#createComponents()
	 */
	@Override
	protected void createComponents() {
		// TODO i18n
		FormData formData = new FormData("-20");
		textName.setFieldLabel("Name");
		textName.setAllowBlank(false);
		textName.addPlugin(plugin);
		textName.setData("text", "Enter the role name");
		textName.getMessages().setBlankText("Name is required");

		textDescription.setFieldLabel("Description");
		textDescription.setAllowBlank(false);
		textDescription.addPlugin(plugin);
		textDescription.setData("text", "Enter the role description");
		textDescription.getMessages().setBlankText("Description is required");

		form.add(textName, formData);
		form.add(textDescription, formData);

		table.setCellPadding(7);
		table.setCellSpacing(7);
		table.setStyleName("table-privileges");

		for (int row = 0; row < modules.length; row++) {
			for (int column = 0; column < actions.length; column++) {
				if (row == 0) {
					table.setText(row, column, actions[column]);

				} else if (column == 0) {
					table.setText(row, column, modules[row]);

				} else {
					table.setWidget(row, column, new CheckBox());
				}
			}
		}
		form.add(new Label("Select the role privileges"), new MarginData(10,
				10, -10, 10));
		form.add(table, formData);

		getPrivileges();
	}

	@Override
	public String getName() {
		return textName.getValue();
	}

	@Override
	public void setName(String name) {
		textName.setValue(name);
	}

	@Override
	public String getDescription() {
		return textDescription.getValue();
	}

	@Override
	public void setDescription(String description) {
		textDescription.setValue(description);
	}

	@Override
	public List<String> getPrivileges() {
		List<String> result = new LinkedList<String>();

		for (int row = 1; row < modules.length; row++) {
			for (int column = 1; column < actions.length; column++) {
				try {
					CheckBox check = (CheckBox) table.getWidget(row, column);
					String key = modules[row].toLowerCase() + "."
							+ actions[column].toLowerCase();
					String value = "no";
					if (check != null) {
						value = (check.getValue()) ? "yes" : "no";
					}
					result.add(key + "=" + value);

				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
		return result;
	}

	@Override
	public void setPrivileges(List<String> privileges) {
		for (int row = 1; row < modules.length; row++) {
			for (int column = 1; column < actions.length; column++) {
				try {
					CheckBox check = (CheckBox) table.getWidget(row, column);
					String key = modules[row].toLowerCase() + "."
							+ actions[column].toLowerCase() + "=yes";
					boolean value = false;
					for (String string : privileges) {
						if (string.equalsIgnoreCase(key)) {
							value = true;
							break;
						}
					}
					if (check != null) {
						check.setValue(value);
					}

				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
	}

}
